﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using SZJT.Framework.Services;

namespace SZJT.Framework.Core
{
    public class PermissionEntity
    {
        private IDictionary<string, Hashtable> UserAuthorityList{get;set;}
        public PermissionEntity(XmlNode roleListNode)
        {
            UserAuthorityList = new Dictionary<string, Hashtable>();
            if (null == roleListNode.SelectNodes("//Role")) return;
            foreach (XmlNode node in roleListNode.SelectNodes("//Role"))
            {
                var appGuid = node.SelectSingleNode("AppGuid ").InnerXml;
                if (string.IsNullOrEmpty(appGuid))
                    throw new Exception("角色信息有误！");
                var memberNodes = node.SelectSingleNode("ListOUDataInfo");
                if (null == memberNodes || !memberNodes.HasChildNodes) continue;
                var authorityNodes = node.SelectSingleNode("ListAuthority");
                if (null == authorityNodes || !authorityNodes.HasChildNodes) continue;
                if (memberNodes.ChildNodes.Count <= 0) continue;
                foreach (XmlNode memberNode in memberNodes.ChildNodes)
                {
                    Hashtable tempAuthority;
                    new Hashtable();
                    if (UserAuthorityList.TryGetValue(memberNode.Attributes["GUID"].Value, out tempAuthority))
                    {
                        tempAuthority = GetAuthority(appGuid, authorityNodes, tempAuthority);
                        UserAuthorityList[memberNode.Attributes["GUID"].Value] = tempAuthority;
                    }
                    else
                        UserAuthorityList.Add(memberNode.Attributes["GUID"].Value, GetAuthority(appGuid, authorityNodes, null));
                }
            }
        }

        /// <summary>
        /// 初始化组件用户权限信息方法
        /// </summary>
        /// <param name="appGuid"></param>
        /// <param name="authorityNodes">权限文档节点</param>
        /// <param name="ht">组件当前用户权限哈希表</param>
        /// <returns>用户权限的哈希表</returns>
        private static Hashtable GetAuthority(string appGuid, XmlNode authorityNodes, Hashtable ht)
        {
            var authorityHt = ht ?? new Hashtable();
            if (authorityNodes.ChildNodes.Count == 0) return authorityHt;
            foreach (XmlNode authorityNode in authorityNodes.ChildNodes)
            {
                if (authorityHt.ContainsKey(appGuid + "|" + authorityNode.Attributes["DataType"].Value))
                    authorityHt[appGuid + "|" + authorityNode.Attributes["DataType"].Value] +=
                        authorityNode.Attributes["Type"].Value;
                else
                    authorityHt.Add(appGuid + "|" + authorityNode.Attributes["DataType"].Value, authorityNode.Attributes["Type"].Value);
            }
            return authorityHt;
        }

        /// <summary>
        /// 根据传入参数判断是否具有相关权限
        /// </summary>
        /// <param name="userId">用户标识</param>
        /// <param name="taskGuid"></param>
        /// <param name="dataType">数据类型</param>
        /// <param name="actionType">访问类型</param>
        /// <returns>[True|False]</returns>
        public bool HasPermission(string userId, string taskGuid, string dataType, ActionType actionType)
        {
            //var userauthority = FsEnvironment.UserAuthority;
            //if (null == userauthority) return true;
            Hashtable authorityHt;
            new Hashtable();
            if (UserAuthorityList.TryGetValue(userId, out authorityHt))
            {
                if (authorityHt.ContainsKey(taskGuid + "|" + dataType) && authorityHt[taskGuid + "|" + dataType].ToString().Contains(actionType.ToString()))
                {
                    //PushPool(userId);
                    return true;
                }
            }
            return false;
        }
    }
}
